<template>
  <div>
 <h1>房型增加</h1>
    <table class="table table-bordered">
      <tbody>
      <tr>
        <td>房屋名称</td>
        <td><input type="text" v-model="info.HouseTypeName"/></td>
      </tr>
      <tr>
        <td>房屋照片</td>
        <td>
          <input type="file" @change="Img"/>
          <img :src="info.Photo" width="150" height="150"/>
        </td>
      </tr>
      <tr>
        <td>房型面积</td>
        <td><input type="text" v-model="info.Area"/></td>
      </tr>
      <tr>
        <td>是否有窗</td>
        <td>
          <input type="radio" name="win" :value="1" v-model="info.IsWin"/>是
          <input type="radio" name="win"  :value="0" v-model="info.IsWin" />否
        </td>
      </tr>
      <tr>
        <td>最多入住人数</td>
        <td><input type="text" v-model="info.PeopleCount"/></td>
      </tr>
      <tr>
        <td>房型价格</td>
        <td><input type="text" v-model="info.Money"/></td>
      </tr>
      <tr>
        <td>房型状态</td>
        <td>
          <input type="radio" name="state"  :value="1" v-model="info.State"/>发布
          <input type="radio" name="state"  :value="0" v-model="info.State"/>不发布
        </td>
      </tr>
      <tr>
        <td></td>
        <td>
          <input type="button" value="确定" class="btn btn-primary" @click="add"/>
        </td>
      </tr>
      </tbody>
    </table>
  </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue'
import { useRouter,useRoute } from 'vue-router'
import moment from "moment";
const router = useRouter()
const route = useRoute()

let info:any=reactive({
  "HouseTypeName": "",
  "Photo": "",
  "Area": '',
  "IsWin": '',
  "PeopleCount": '',
  "Money": '',
  "State": 1,
})
const Img=(e:any) => {
 let obj=e.target.files[0];
 let f=new FormData();
 f.append('file', obj);
 axios({
   url:'/api/Img/Img',
   method:'post',
   data:f
 }).then((res) => {
   console.log(res)
   if(res.data=="不是图片"){
     alert("您上传的格式有误")
     return;
   }
   if(res.data=="大于2M")
   {
     alert("您上传的过大")
   }
   info.Photo=res.data

 })
}
const add=()=>{
if(info.HouseTypeName == ""){
  alert("房型不能为空")
  return;
}
if(info.Photo==""){
  alert("房型图片不能为空")
  return;
}
  let reg1 = /^[0-9]+(\.[0-9]+)?$/;
if(reg1.test(info.Area)==false)
{
  alert("房型面积只能是正数")
  return;
}
if(info.Money == ""){
  alert("房型价格不能为空")
  return;
}
axios({
  url:'/api/House/HouseTypeAdd',
  method:'post',
  data:info,
}).then((res) => {
  if(res.data==-1){
    alert("房型重复")
    return;
  }
  if(res.data>0){
    alert("添加成功")
    return;
  }
  else {
    alert("添加失败")
    return;
  }
})
}
</script>

<style lang="scss" scoped>

</style>